package com.musicmall.controller.portal;

import com.github.pagehelper.PageInfo;
import com.musicmall.common.Const;
import com.musicmall.common.ResponseCode;
import com.musicmall.common.ServerResponse;
import com.musicmall.pojo.User;
import com.musicmall.service.ISinglesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/singles")
public class SinglesController {
    @Autowired
    private ISinglesService iSinglesService;

    @GetMapping("list.do")
    public ServerResponse<PageInfo> list(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                         @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
//        return iProductService.getProductByKeywordCategory(keyword,categoryId,pageNum,pageSize,orderBy);
        return iSinglesService.getSinglesList(pageNum,pageSize);
    }

    @GetMapping("search.do")
    public ServerResponse searchSingles(String keyword){
        return iSinglesService.searchSingles(keyword);
    }

    @PostMapping("add_fav.do")
    public ServerResponse addFav(HttpSession session, Integer singlesId){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
                    ResponseCode.NEED_LOGIN.getDesc());
        }
        return iSinglesService.addFav(user.getId(),singlesId);

    }

    @PostMapping("del_fav.do")
    public ServerResponse delFav(HttpSession session,Integer singlesId){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
                    ResponseCode.NEED_LOGIN.getDesc());
        }
        return iSinglesService.delFav(user.getId(),singlesId);
    }




}
